package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BanknoteSerialNumberRecord;
import com.ruoyi.system.domain.GroupResult;
import com.ruoyi.system.mapper.BanknoteSerialNumberRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BanknoteSerialNumberPairingMapper;
import com.ruoyi.system.domain.BanknoteSerialNumberPairing;
import com.ruoyi.system.service.IBanknoteSerialNumberPairingService;

import static com.ruoyi.system.service.impl.UserMoneyServiceImpl.*;

/**
 * 冠字号配对Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class BanknoteSerialNumberPairingServiceImpl implements IBanknoteSerialNumberPairingService 
{
    @Autowired
    private BanknoteSerialNumberPairingMapper banknoteSerialNumberPairingMapper;
    @Autowired
    private BanknoteSerialNumberRecordMapper banknoteSerialNumberRecordMapper;
    /**
     * 查询冠字号配对
     * 
     * @param id 冠字号配对主键
     * @return 冠字号配对
     */
    @Override
    public BanknoteSerialNumberPairing selectBanknoteSerialNumberPairingById(Long id)
    {
        return banknoteSerialNumberPairingMapper.selectBanknoteSerialNumberPairingById(id);
    }

    /**
     * 查询冠字号配对列表
     * 
     * @param banknoteSerialNumberPairing 冠字号配对
     * @return 冠字号配对
     */
    @Override
    public List<BanknoteSerialNumberRecord> selectBanknoteSerialNumberPairingList(BanknoteSerialNumberPairing banknoteSerialNumberPairing)
    {
        //获取我的配号
        List<BanknoteSerialNumberPairing> list = banknoteSerialNumberPairingMapper.selectBanknoteSerialNumberPairingList(banknoteSerialNumberPairing);
        //获取这些号码
        if (list.isEmpty()){
            return Collections.emptyList();
        }
        //根据billtye分类
        Map<Integer, List<BanknoteSerialNumberPairing>> map = list.stream().collect(Collectors.groupingBy(BanknoteSerialNumberPairing::getBillType));
        //获取对应的匹配号码
        List<BanknoteSerialNumberRecord> myRecords = new ArrayList<>();
        List<BanknoteSerialNumberRecord> otherRecords = new ArrayList<>();

        map.forEach((key,value)->{
            //获取value的号码列表
            List<String> codes = value.stream().map(BanknoteSerialNumberPairing::getSerialNumber).collect(Collectors.toList());

            //获取我自己的号码
            myRecords.addAll( banknoteSerialNumberRecordMapper.selectByTypeAndCodes(key, codes));

            //获取对方的
            if (key == 0){
                otherRecords.addAll(banknoteSerialNumberRecordMapper.selectByTypeAndCodes(1, codes));
            }else {
                otherRecords.addAll(banknoteSerialNumberRecordMapper.selectByTypeAndCodes(0, codes));
            }
        });


        //myRecords根据号码 分组 list
        Map<String, List<BanknoteSerialNumberRecord>> myRecordsMap = myRecords.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getSerialNumber));
        Map<String, List<BanknoteSerialNumberRecord>> otherRecordsMap = otherRecords.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getSerialNumber));
        myRecordsMap.forEach((key,value)->{
            List<BanknoteSerialNumberPairing> collect = list.stream().filter(item -> item.getSerialNumber().equals(key)).collect(Collectors.toList());
            Date peiTime = new Date();
            if (!collect.isEmpty()){
                peiTime = collect.get(0).getPeiTime();
            }
            if (otherRecordsMap.containsKey(key)){
                if (value.size() == 1){
                    BanknoteSerialNumberRecord otherRecord = otherRecordsMap.get(key).get(0);
                    value.get(0).setOtherOwner(otherRecord.getNumberOwnerAccount());
                    value.get(0).setOtherBillType(otherRecord.getBillType());
                    value.get(0).setOtherNumberType(otherRecord.getNumberType());
                    value.get(0).setOtherGradedScore(otherRecord.getGradedScore());
                    value.get(0).setOtherNakedOrGraded(otherRecord.getNakedOrGraded());
                    value.get(0).setPeiTime(peiTime);
                }else {
                    List<BanknoteSerialNumberRecord> otherRecordList = otherRecordsMap.get(key);
                    BanknoteSerialNumberRecord otherRecord = new BanknoteSerialNumberRecord();
                    //找到类型不一样的
                    if (value.get(0).getBillType() == 1){
                        //找打相反的
                        otherRecord = otherRecordList.stream().filter(item -> item.getBillType() == 0).findFirst().get();
                    }else {
                        otherRecord = otherRecordList.stream().filter(item -> item.getBillType() == 1).findFirst().get();
                    }


                    value.get(0).setOtherOwner(otherRecord.getNumberOwnerAccount());
                    value.get(0).setOtherBillType(otherRecord.getBillType());
                    value.get(0).setOtherNumberType(otherRecord.getNumberType());
                    value.get(0).setOtherGradedScore(otherRecord.getGradedScore());
                    value.get(0).setOtherNakedOrGraded(otherRecord.getNakedOrGraded());
                    value.get(0).setPeiTime(peiTime);


                    BanknoteSerialNumberRecord otherRecord1 = new BanknoteSerialNumberRecord();
                    //找到类型不一样的
                    if (value.get(1).getBillType() == 1){
                        //找打相反的
                        otherRecord1 = otherRecordList.stream().filter(item -> item.getBillType() == 0).findFirst().get();
                    }else {
                        otherRecord1 = otherRecordList.stream().filter(item -> item.getBillType() == 1).findFirst().get();
                    }
                    value.get(1).setOtherOwner(otherRecord1.getNumberOwnerAccount());
                    value.get(1).setOtherBillType(otherRecord1.getBillType());
                    value.get(1).setOtherNumberType(otherRecord1.getNumberType());
                    value.get(1).setOtherGradedScore(otherRecord1.getGradedScore());
                    value.get(1).setOtherNakedOrGraded(otherRecord1.getNakedOrGraded());
                    value.get(1).setPeiTime(peiTime);
                }



            }
        });


        //myRecordsMap获取list
        List<BanknoteSerialNumberRecord> returnList = new ArrayList<>();
        myRecordsMap.forEach((key,value)->{
            returnList.addAll(value);
        });

        //过滤掉otherOwner为空的
        returnList.removeIf(item->{
            return item.getOtherOwner() == null;
        });
        //根据项目类别进行分类
        Map<Long,List<BanknoteSerialNumberRecord>> typeMap = returnList.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getNumberType));
        List<BanknoteSerialNumberRecord> listAll = new ArrayList<>();

        //处理单张数据
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords1 = typeMap.get(0L);
        if (banknoteSerialNumberRecords1 != null){
            listAll.addAll(banknoteSerialNumberRecords1);
        }


        //处理标十数据
        //按大小排序
        returnList.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords2 = typeMap.get(1L);
        if (banknoteSerialNumberRecords2 != null){
            //分组
            Map<Integer, List<BanknoteSerialNumberRecord>> typeMap1 = banknoteSerialNumberRecords2.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
                //帮我找出每个十相连的数据
                List<GroupResult> groupResults = groupSerialNumbers(value);
                for (GroupResult groupResult : groupResults) {
                    BanknoteSerialNumberRecord firstObject = groupResult.getFirstObject();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getList().stream().map(BanknoteSerialNumberRecord::getId).collect(Collectors.toList()));

                    listAll.add(firstObject);
                }
            });


        }

        //处理标百数据
        returnList.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords3 = typeMap.get(2L);
        if (banknoteSerialNumberRecords3 != null){
            //分组
            Map<Integer, List<BanknoteSerialNumberRecord>> typeMap1 = banknoteSerialNumberRecords3.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumbersTens(value);
                for (GroupResult groupResult : groupResults) {
                    BanknoteSerialNumberRecord firstObject = groupResult.getFirstObject();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getList().stream().map(BanknoteSerialNumberRecord::getId).collect(Collectors.toList()));
                    listAll.add(firstObject);
                }
            });

        }


        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords4 = typeMap.get(3L);
        if (banknoteSerialNumberRecords4 != null){
            //分组
            Map<Integer, List<BanknoteSerialNumberRecord>> typeMap1 = banknoteSerialNumberRecords4.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumberss(value);
                for (GroupResult groupResult : groupResults) {
                    BanknoteSerialNumberRecord firstObject = groupResult.getFirstObject();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getList().stream().map(BanknoteSerialNumberRecord::getId).collect(Collectors.toList()));

                    listAll.add(firstObject);
                }
            });

        }

        return listAll;
    }

    /**
     * 新增冠字号配对
     * 
     * @param banknoteSerialNumberPairing 冠字号配对
     * @return 结果
     */
    @Override
    public int insertBanknoteSerialNumberPairing(BanknoteSerialNumberPairing banknoteSerialNumberPairing)
    {
        banknoteSerialNumberPairing.setPeiTime(new Date());
        return banknoteSerialNumberPairingMapper.insertBanknoteSerialNumberPairing(banknoteSerialNumberPairing);
    }

    /**
     * 修改冠字号配对
     * 
     * @param banknoteSerialNumberPairing 冠字号配对
     * @return 结果
     */
    @Override
    public int updateBanknoteSerialNumberPairing(BanknoteSerialNumberPairing banknoteSerialNumberPairing)
    {
        return banknoteSerialNumberPairingMapper.updateBanknoteSerialNumberPairing(banknoteSerialNumberPairing);
    }

    /**
     * 批量删除冠字号配对
     * 
     * @param ids 需要删除的冠字号配对主键
     * @return 结果
     */
    @Override
    public int deleteBanknoteSerialNumberPairingByIds(Long[] ids)
    {
        return banknoteSerialNumberPairingMapper.deleteBanknoteSerialNumberPairingByIds(ids);
    }

    /**
     * 删除冠字号配对信息
     * 
     * @param id 冠字号配对主键
     * @return 结果
     */
    @Override
    public int deleteBanknoteSerialNumberPairingById(Long id)
    {
        return banknoteSerialNumberPairingMapper.deleteBanknoteSerialNumberPairingById(id);
    }

    @Override
    public void insertList(List<BanknoteSerialNumberPairing> list) {
        list.forEach(banknoteSerialNumberPairing -> {
            banknoteSerialNumberPairing.setPeiTime(new Date());
        });
        banknoteSerialNumberPairingMapper.insertList(list);
    }

    @Override
    public List<BanknoteSerialNumberPairing> selectByCodes(Integer key, List<String> codes) {
        return banknoteSerialNumberPairingMapper.selectByCodes(key, codes);
    }

    @Override
    public List<BanknoteSerialNumberPairing> list(BanknoteSerialNumberPairing banknoteSerialNumberPairing) {
        return banknoteSerialNumberPairingMapper.selectBanknoteSerialNumberPairingList(banknoteSerialNumberPairing);
    }

    @Override
    public void deleteByAndCodes(List<String> collect) {
        banknoteSerialNumberPairingMapper.deleteByAndCodes(collect);
    }
}
